library(ggplot2)
library(dplyr)
library(tidyverse)
library(tidyr)
library(lubridate)
library(devtools)
library(magrittr)
library(wesanderson)
library(patchwork) 
library(modelsummary)
library(vdemdata) # access to V-Dem & V-Party. 
# Note: The analysis published at BJPS was done on V-Dem dataset v14 and V-Party dataset v2
library(lme4) 
library(interplot) 
library(ggrepel) 
library(brglm) 
library(brglm2) 
library(scales)
library(ggpubr)
library(MASS)
library(here)
library(readxl)
library(ggridges)
library(gghighlight)

### Setting up ####
## upload ERT from Github repository in case package doesn't work 
ert <- read_csv("https://raw.githubusercontent.com/vdeminstitute/ERT/master/inst/ert.csv",
                locale = locale(encoding ="latin1"))
vparty.ert <- left_join(vparty, ert, by = c("country_name", "year"))
vdem <- vdem%>%
  filter(year>1969 & year<2020)
vall <- left_join(vparty.ert,vdem, by = c("country_name", "year"))
vall <- vall%>%
  filter(year>1969)
# variable on whether the party is inn opposition (3) or government (1 or 2)
# 1 = governemnt, 0 = opposition
vall$v2pagovsup.dum <- NA
vall$v2pagovsup.dum[vall$v2pagovsup == 0] <- 1
vall$v2pagovsup.dum[vall$v2pagovsup == 1] <- 1
vall$v2pagovsup.dum[vall$v2pagovsup == 2] <- 1
vall$v2pagovsup.dum[vall$v2pagovsup == 3] <- 0

## replacing NAs on seatshare for Democratic social Party Brazil with A data manually
## NOTE TO READER: This passage is important for research on Brazil as the years between
## 1980-1990 overlaps with the democratization of the country. Out of the many political
## parties that existed at the time, only the A-data on seat share of the Democratic Social Party
## is missing from V-Party. Leaving these observations as NAs would result in dropping three 
## key years. We replace them manually using the following source:
## Dieter Nohlen (2005) Elections in the Americas: A data handbook, Volume II

vall <- vall %>% 
  mutate(v2paseatshare = replace(v2paseatshare, v2paenname=="Democratic Social Party" & year==1982, 43.22),
         v2paseatshare = replace(v2paseatshare, v2paenname=="Democratic Social Party" & year==1986, 7.89),
         v2paseatshare = replace(v2paseatshare, v2paenname=="Democratic Social Party" & year==1990, 8.91)
  )

vall$seatshare.1 <- vall$v2paseatshare*0.01
vall$pavote.1 <- vall$v2pavote*0.01

####• Creating our new index: Party-System Democracy Index •####
psdi.raw <- vall %>%   dplyr::select(country_id, country_text_id, e_regionpol_6C.x,
                                     country_name, year,v2pagovsup.dum, 
                                     v2x_polyarchy.x,v2x_polyarchy_codelow.x,v2x_polyarchy_codehigh.x,
                                     seatshare.1,
                                     v2xpa_antiplural) %>%
  dplyr::group_by(country_id, country_text_id, e_regionpol_6C.x,
                  country_name, year,v2pagovsup.dum,
                  v2x_polyarchy.x,v2x_polyarchy_codelow.x,v2x_polyarchy_codehigh.x) %>% 
  dplyr::summarise(
    wm_autpol = weighted.mean(v2xpa_antiplural,seatshare.1),
    .groups = 'drop') %>%
  as.data.frame()

psdi.data <- psdi.raw %>%
  spread(v2pagovsup.dum,wm_autpol) 
# rescale governmment and opposition to 0 to 1
psdi.data$v2ps_democracy_opposition <- psdi.data$`0`
# if there is no opposition, then we force a 0 (= opposition is not allowed to run during elections)
psdi.data$v2ps_democracy_opposition_adj <- ifelse(is.na(psdi.data$v2ps_democracy_opposition), 0, 
                                                  psdi.data$v2ps_democracy_opposition)
psdi.data$v2ps_democracy_government <- psdi.data$`1`

# Rescaling and capturing one-party systems
psdi.data$psdi <- 1-(psdi.data$v2ps_democracy_government+psdi.data$v2ps_democracy_opposition_adj)
psdi.data$v2ps_democracy <- rescale(psdi.data$psdi, to=c(0,1))
psdi.data  <- psdi.data%>%
  dplyr::mutate(v2ps_democracy_adj = ifelse(v2x_polyarchy.x>.5, v2ps_democracy, "NA")) %>%
  dplyr::mutate(v2ps_democracy_adj = ifelse(v2x_polyarchy.x<=.5 & psdi.data$v2ps_democracy_opposition_adj ==0, 
                                            0, v2ps_democracy))%>%
  as.data.frame()
### PSDI CODE LOW ####
psdi.raw.low <- vall %>%   dplyr::select(country_id, country_text_id, e_regionpol_6C.x,
                                         country_name, year,v2pagovsup.dum, 
                                         v2x_polyarchy_codelow.x,
                                         seatshare.1,
                                         v2xpa_antiplural_codelow) %>%
  dplyr::group_by(country_id, country_text_id, e_regionpol_6C.x,
                  country_name, year,v2pagovsup.dum,
                  v2x_polyarchy_codelow.x) %>% 
  dplyr::summarise(
    wm_autpol.low = weighted.mean(v2xpa_antiplural_codelow,seatshare.1),
    .groups = 'drop') %>%
  as.data.frame()

psdi.data.low <- psdi.raw.low %>%
  spread(v2pagovsup.dum,wm_autpol.low) 
# rescale governmment and opposition to 0 to 1
psdi.data.low$v2ps_democracy_opposition_codelow <- psdi.data.low$`0`
# if there is no opposition, then we force a 0 (= opposition is not allowed to run)
psdi.data.low$v2ps_democracy_opposition_codelow_adj <- ifelse(is.na(psdi.data.low$v2ps_democracy_opposition_codelow), 0, 
                                                              psdi.data.low$v2ps_democracy_opposition_codelow)
psdi.data.low$v2ps_democracy_government_codelow <- psdi.data.low$`1`

# Rescaling and capturing one-party systems
psdi.data.low$psdi_low <- 1-(psdi.data.low$v2ps_democracy_government_codelow+psdi.data.low$v2ps_democracy_opposition_codelow_adj)
psdi.data.low$v2ps_democracy_codelow <- rescale(psdi.data.low$psdi_low, to=c(0,1))
psdi.data.low  <- psdi.data.low%>%
  dplyr::mutate(v2ps_democracy_codelow_adj = ifelse(v2x_polyarchy_codelow.x>.59, v2ps_democracy_codelow, "NA")) %>%
  dplyr::mutate(v2ps_democracy_codelow_adj = ifelse(v2x_polyarchy_codelow.x<=.59 & psdi.data.low$v2ps_democracy_opposition_codelow_adj ==0, 
                                                    0, v2ps_democracy_codelow))%>%
  as.data.frame()
psdi.data.low$v2ps_democracy_codelow_adj[psdi.data.low$country_name == "Cyprus" & psdi.data.low$year ==1976] <- 0.8179024 # this is due to forcing variable challenges and replaces the v2ps_democracy_codelow_adj with v2ps_democracy_codelow

### PSDI CODE HIGH ####
psdi.raw.high <- vall %>%   dplyr::select(country_id, country_text_id, e_regionpol_6C.x,
                                          country_name, year,v2pagovsup.dum, 
                                          v2x_polyarchy_codehigh.x,
                                          seatshare.1,
                                          v2xpa_antiplural_codehigh) %>%
  dplyr::group_by(country_id, country_text_id, e_regionpol_6C.x,
                  country_name, year,v2pagovsup.dum,
                  v2x_polyarchy_codehigh.x) %>% 
  dplyr::summarise(
    wm_autpol.high = weighted.mean(v2xpa_antiplural_codehigh,seatshare.1),
    .groups = 'drop') %>%
  as.data.frame()

psdi.data.high <- psdi.raw.high%>%
  spread(v2pagovsup.dum,wm_autpol.high) 

# rescale governmment and opposition to 0 to 1
psdi.data.high$v2ps_democracy_opposition_codehigh <- psdi.data.high$`0`
psdi.data.high$v2ps_democracy_government_codehigh <- psdi.data.high$`1`
psdi.data.high$v2ps_democracy_opposition_codehigh_adj <- ifelse(is.na(psdi.data.high$v2ps_democracy_opposition_codehigh), 
                                                                0, psdi.data.high$v2ps_democracy_opposition_codehigh)

# Rescaling and capturing one-party systems
psdi.data.high$psdi_high <- 1-(psdi.data.high$v2ps_democracy_government_codehigh+psdi.data.high$v2ps_democracy_opposition_codehigh_adj)
psdi.data.high$v2ps_democracy_codehigh <- rescale(psdi.data.high$psdi_high, to=c(0,1))
psdi.data.high  <- psdi.data.high%>%
  dplyr::mutate(v2ps_democracy_codehigh_adj = ifelse(v2x_polyarchy_codehigh.x>.59, v2ps_democracy_codehigh, "NA")) %>%
  dplyr::mutate(v2ps_democracy_codehigh_adj = ifelse(v2x_polyarchy_codehigh.x<=.59 & v2ps_democracy_opposition_codehigh_adj ==0, 
                                                     0, v2ps_democracy_codehigh))%>%
  as.data.frame()

### Merges all PSDI measures ####
psdi.data.a <- left_join(psdi.data, psdi.data.high, by = c("country_name", "year")) %>%
  left_join(., psdi.data.low, by = c("country_name", "year"))

psdi.data.clean <- psdi.data.a%>%
  dplyr::select('country_id','country_name', 'country_text_id', 'year', 'e_regionpol_6C.x',
                'v2ps_democracy_adj', 'v2ps_democracy_opposition_adj', 'v2ps_democracy_government',
                'v2ps_democracy_codehigh_adj', 'v2ps_democracy_opposition_codehigh_adj', 'v2ps_democracy_government_codehigh',
                'v2ps_democracy_codelow_adj', 'v2ps_democracy_opposition_codelow_adj', 'v2ps_democracy_government_codelow')

# Rename Czech Republic to Czechia to allow this country to be merged with V-Dem data
psdi.data.clean$country_name[psdi.data.clean$country_name == 'Czech Republic'] <- 'Czechia'
